<?php
/**
 * Created by PhpStorm.
 * User: yuxingkui
 * Date: 2017/8/17
 * Time: 下午4:26
 */
namespace App\Api\Helper;

use App\Models\cocloud\StaffStyle;
use Dingo\Api\Http\Response;

class ReturnMessage
{
	/**
	 * 操作成功返回
	 * @param $code
	 * @param $info
	 * @param mixed $data
	 * @return Response
	 */
	public static function success($info='success', $code = "1000", $data = false )
	{
		$responseData = [
			'code' => (string)$code,
			'info' => $info,
		];

		if ($data !== false && is_array($data)){
			$responseData['data'] =self::toString($data);
		}elseif(is_object($data)){
			$responseData['data']=$data;
		}else{
			$responseData['data']='';
		}

		//如果返回有返回错误，记录日志
        self::setErrorlog($info,$code);
		return new Response($responseData);
	}

	/**
	 * @param string $info
	 * @param string $code
	 * @param bool|false $data
	 * @return Response
	 * @name 操作成功返回 App使用
	 * @user IHammer
	 */
	public static function successToApp($info='success', $code = "1000", $data = false )
	{
		$responseData = [
				'code' => (string)$code,
				'info' => $info,
		];

		if ($data !== false && is_array($data)){
			if(!empty($data)){
				$responseData['data'] = self::toString($data);
			}else{
				$responseData['data'] = $data;
			}
		}elseif(is_object($data)){
			if(!empty($data)){
				$responseData['data'] = @self::toString($data);
			}else{
				$responseData['data'] = $data;
			}

		}else{
			$responseData['data']='';
		}

		//如果返回有返回错误，记录日志
		self::setErrorlog($info,$code);
		return new Response($responseData);
	}

	//记录错误日志
	private static function setErrorlog($info,$code){
        if($code!='1000') logStore('error',$info);
    }

	/**
	 * 只返回数据
	 * @param $data
	 * @param $info
	 * @return Response
	 */
	public static function successData($data, $info='success')
	{
		$code = "1000";

		if (is_object($data))
			$data = $data->toArray();

		if (!is_array($data) || empty($data)) {
			$info = '暂无数据';
			$data = false;
		}
		return self::success($info, $code, $data);
	}


	/**
	 * Miyu 列表使用
	 * @param $data
	 * @param $info
	 * @return Response
	 */
	public static function successLists($data, $info='success')
	{
		$code = "1000";

		if (is_object($data))
			$data = $data->toArray();

		if (!is_array($data) || empty($data)) {
			$info = '暂无数据';
			$data = ['data'=>[]];
		}
		return self::success($info, $code, $data);
	}
	/**
	 * @param $data
	 * @param string $info
	 * @return Response
	 * @name  只返回数据 查询数据专用
	 * @user IHammer
	 */
	public static function successArray($data, $info='success')
	{
		$code = "1000";

		if (is_object($data))
			$data = $data->toArray();

		if (!is_array($data) || empty($data)) {
			$obj = new \stdClass();
			$data = $data;
		}
		return self::success($info, $code, $data);
	}

	/**
	 * @param $data
	 * @param string $info
	 * @return Response
	 * @name 仅提供App使用
	 * @user IHammer
	 */
	public static function successArrayToApp($data, $info='success')
	{
		$code = "1000";

		if (is_object($data)){
			$data = $data->toArray();
		}
		if (!is_array($data) || empty($data)) {
			$obj = new \stdClass();
			$data = $obj;
		}
		return self::successToApp($info, $code, $data);
	}
	/**
	 * 返回数组值全部为字符串
	 * @param array $data
	 * @return array
	 */
	protected static function toString($data)
	{
		if(!is_array($data)){
			return $data;
		}
		foreach ($data as $key => $value) {
			if (is_array($value)){
				$data[$key] = self::toString($value);
			}elseif (!is_string($value)&&!is_object($value)){
				$data[$key] = (string) $value;
			}else{
				$data[$key] = $value;
			}
		}
		return $data;
	}

	/**
	 * @param $data
	 * @param string $info
	 * @return Response
	 * @name 分页使用
	 * @user IHammer
	 */
	public static function successToAppPage($data, $info='success'){
		$code = "1000";
		if (is_object($data)){
			$data = $data->toArray();
		}
		if (!is_array($data) || empty($data)) {
			$obj = new \stdClass();
			$data = $obj;
		}
		foreach ($data as $key => $value) {
			if (is_array($value)){
				$data[$key] = self::toString($value);
			}elseif (!is_string($value)&&!is_object($value)){
				$data[$key] = (string) $value;
			}else{
				$data[$key] = $value;
			}
		}
		$responseData = [
				'code' => (string)$code,
				'info' => $info,
			 	'data' => $data
		];
		return new Response($responseData);
	}

	public static function successToAppInfo($data, $info='success'){
		$code = "1000";
		if (is_object($data)){
			$data = $data->toArray();
		}
		if (!is_array($data) || empty($data)) {
			$obj = new \stdClass();
			$data = $obj;
		}
		foreach ($data as $key => $value) {
			if (is_array($value)){
				$data[$key] = self::toString($value);
			}elseif($value===null){
				$data[$key] = new \stdClass();
			}elseif (!is_string($value)&&!is_object($value)){
				$data[$key] = (string) $value;
			}else{
				$data[$key] = $value;
			}
		}
		$responseData = [
				'code' => (string)$code,
				'info' => $info,
				'data' => $data
		];
		return new Response($responseData);
	}



}




